package com.example.xianzhi.dao.impl;

import com.example.xianzhi.dao.RelationDao;
import com.example.xianzhi.util.ORMUtil;

public class RelationDaoImpl implements RelationDao {
    @Override
    public int selectRelation(int user_id, int goods_id) {
        String sql="SELECT count(*) jls FROM `tb_relation` WHERE user_id=? AND goods_id=?";
        long jls = (long) new ORMUtil().selectSingleValue("jls",sql,user_id,goods_id);
        return (int) jls;
    }

    @Override
    public void updateRelation(int user_id, int goods_id, int need) {
        String sql="update tb_relation set need=? where user_id=? AND goods_id=?";
        new ORMUtil().executeDML(sql,need,user_id,goods_id);
    }

    @Override
    public void insertRelation(int user_id, int goods_id, int need) {
        String sql="insert into tb_relation(user_id, goods_id, need)  values(?, ?, ? );";
        new ORMUtil().executeDML(sql,user_id,goods_id,need);
    }

    @Override
    public int selectNeed(int user_id, int goods_id) {
        String sql ="SELECT need jls  FROM `tb_relation` WHERE user_id=? AND goods_id=?";
        int jls =0;
        System.out.println(new ORMUtil().selectSingleValue("jls",sql,user_id,goods_id));
        if(new ORMUtil().selectSingleValue("jls",sql,user_id,goods_id)!=null) {
            Object obj = new ORMUtil().selectSingleValue("jls", sql, user_id, goods_id);
            jls = Integer.parseInt(obj.toString());
        }
        return jls;
    }


    @Override
    public int deleterela(int relation_id) {
        String sql =" delete from  tb_relation where relation_id=? ";



        return new ORMUtil().executeDML(sql, relation_id);
    }
}
